<?php namespace Home\Controller;

//工作台控制器
class WorkbenchController extends CheckController{
	private $model;//存放模型对象
	private $gsid;//存放模型对象
	//自动执行函数
	public function __doit(){
		//实例化职位表
		$this->model=new \Common\Model\Job;
		//获得公司id
		$uid=$_SESSION['homeInfo']['uid'];
		$companyModel=new \Common\Model\Company;
		$this->gsid=$companyModel->where("lg_user_uid={$uid}")->pluck('gsid');
	}

	//工作台页面（发布职位）
	public function index(){
		if (IS_POST) {
			//获取工作地点的城市id
			$city_name=Q('post.city_name');
			$city_id=Db::table('city')->where("city_name='{$city_name}'")->pluck('city_id');
			if (!$city_id) View::error('该城市尚未开通招聘服务');
			$_POST['lg_city_city_id']=$city_id;
			//获得所选分类的id
			$cname=Q('post.cname');
			$cid=Db::table('category')->where("cname='{$cname}'")->pluck('cid');
			if (!$cid) View::error('该分类不存在');
			$_POST['category_cid']=$cid;
			//将职位添加到职位表
			if (!$this->model->store()) View::error($this->model->getError());
			View::success('发布成功',__ROOT__.'/online.html');
		}

		//获得分类数据
		$cateModel=new \Common\Model\Cate;
		$cateData=Cache::get('cateData');
		if (!$cateData) {
			$cateData=$cateModel->where("pid=0")->get();
			foreach ($cateData as $key => $value) {
				$cateData[$key]['son']=$cateModel->where("pid={$value['cid']}")->get();
				foreach ($cateData[$key]['son'] as $k => $v) {
					$cateData[$key]['son'][$k]['son']=$cateModel->where("pid={$v['cid']}")->get();
				}
			}
			Cache::set('cateData',$cateData);
		}
		View::with('cateData',$cateData);
		//分配公司id
		View::with('gsid',$this->gsid);
		View::make();
	}

	//待处理简历
	public function waitDeal(){
		$jids=$this->model->where("lg_company_gsid={$this->gsid}")->lists('jid');
		$jids=$jids?implode(',', $jids):'0';
		$statusModel=new \Common\Model\Status;
		$data=$statusModel
			->join('user','uid','=','lg_user_uid')
			->join('job','jid','=','lg_job_jid')
			->where("lg_job_jid IN ({$jids}) AND status=0 AND recycle=0")
			->orderBy('delivertime','DESC')
			->get();
		$num=count($data);
		View::with('data',$data);
		View::with('num',$num);
		View::make();
	}

	//待定简历
	public function waitReply(){
		$jids=$this->model->where("lg_company_gsid={$this->gsid}")->lists('jid');
		$jids=$jids?implode(',', $jids):'0';
		$statusModel=new \Common\Model\Status;
		$data=$statusModel
			->join('user','uid','=','lg_user_uid')
			->join('job','jid','=','lg_job_jid')
			->where("lg_job_jid IN ({$jids}) AND status=1 AND recycle=0")
			->orderBy('delivertime','DESC')
			->get();
		$num=count($data);
		View::with('data',$data);
		View::with('num',$num);
		View::make();
	}

	//已通知面试简历
	public function facetime(){
		$jids=$this->model->where("lg_company_gsid={$this->gsid}")->lists('jid');
		$jids=$jids?implode(',', $jids):'0';
		$statusModel=new \Common\Model\Status;
		$data=$statusModel
			->join('user','uid','=','lg_user_uid')
			->join('job','jid','=','lg_job_jid')
			->where("lg_job_jid IN ({$jids}) AND status=2 AND recycle=0")
			->orderBy('delivertime','DESC')
			->get();
		$num=count($data);
		View::with('data',$data);
		View::with('num',$num);
		View::make();
	}

	//不合适简历
	public function mismatch(){
		$jids=$this->model->where("lg_company_gsid={$this->gsid}")->lists('jid');
		$jids=$jids?implode(',', $jids):'0';
		$statusModel=new \Common\Model\Status;
		$data=$statusModel
			->join('user','uid','=','lg_user_uid')
			->join('job','jid','=','lg_job_jid')
			->where("lg_job_jid IN ({$jids}) AND status=3 AND recycle=0")
			->orderBy('delivertime','DESC')
			->get();
		$num=count($data);
		View::with('data',$data);
		View::with('num',$num);
		View::make();
	}

	//有效职位
	public function online(){
		//获得公司的上线职位
		$data=$this->model->where("lg_company_gsid={$this->gsid} and online=1")->orderBy('jobsendtime','DESC')->get();
		$dataNum=count($data);
		View::with('data',$data);
		View::with('dataNum',$dataNum);
		View::make();
	}

	//已下线职位
	public function offline(){
		//获得公司的下线职位
		$data=$this->model->where("lg_company_gsid={$this->gsid} and online=0")->get();
		$dataNum=count($data);
		View::with('data',$data);
		View::with('dataNum',$dataNum);
		View::make();
	}

	//职位下线操作
	public function getOff(){
		//获得要下线职位的id
		$jid=Q('get.jid');
		//下线职位
		$this->model->where("jid={$jid}")->update(['online'=>0]);
		//公司职位数量自增
		$companyModel=new \Common\Model\Company;
		$uid=$_SESSION['homeInfo']['uid'];
		$companyModel->where('lg_user_uid',$uid)->decrement('jobnumber',1);
		View::success('下线成功',__ROOT__.'/offline.html');
	}

	//重新上线操作
	public function getOn(){
		//获得要重新上线职位的id
		$jid=Q('get.jid');
		//上线职位
		$this->model->where("jid={$jid}")->update(['online'=>1,'jobsendtime'=>time()]);
		//公司职位数量自增
		$companyModel=new \Common\Model\Company;
		$uid=$_SESSION['homeInfo']['uid'];
		$companyModel->where('lg_user_uid',$uid)->increment('jobnumber',1);
		View::success('重新发布成功',__ROOT__.'/online.html');
	}

	//删除已下线职位
	public function del(){
		//获得要下线职位的id
		$jid=Q('get.jid');
		//删除职位信息
		$this->model->where("jid={$jid}")->delete();
		Db::table('job_data')->where("lg_job_jid={$jid}")->delete();
		View::success('删除成功');
	}

	//编辑职位
	public function edit(){
		if (IS_POST) {
			//获取工作地点的城市id
			$city_name=Q('post.city_name');
			$city_id=Db::table('city')->where("city_name='{$city_name}'")->pluck('city_id');
			if (!$city_id) View::error('该城市尚未开通招聘服务');
			$_POST['lg_city_city_id']=$city_id;
			//获得所选分类的id
			$cname=Q('post.cname');
			$cid=Db::table('category')->where("cname='{$cname}'")->pluck('cid');
			if (!$cid) View::error('该分类不存在');
			$_POST['category_cid']=$cid;
			if (!$this->model->edit()) View::error($this->getError());
			View::success('编辑成功',__ROOT__.'/online.html');
		}
		//获得分类数据
		$cateModel=new \Common\Model\Cate;
		$cateData=$cateModel->where("pid=0")->get();
		foreach ($cateData as $key => $value) {
			$cateData[$key]['son']=$cateModel->where("pid={$value['cid']}")->get();
			foreach ($cateData[$key]['son'] as $k => $v) {
				$cateData[$key]['son'][$k]['son']=$cateModel->where("pid={$v['cid']}")->get();
			}
		}
		//获得要编辑职位的id
		$jid=Q('get.jid');
		$data=$this->model
					->join('job_data','jid','=','lg_job_jid')
					->where("jid={$jid}")
					->find();
		$cname=$cateModel->where("cid={$data['category_cid']}")->pluck('cname');
		//获得分类名称
		$data['cname']=$cname;
		//拆分薪酬
		$salary=explode('-',str_replace('k','',$data['salary']));
		$data['money_low']=$salary[0];
		$data['money_up']=$salary[1];
		//获得工作地点联动信息
		$cityModel=new \Common\Model\City;
		$area=$cityModel->where("city_id={$data['lg_city_city_id']}")->find();
		$city=$cityModel->where("city_id={$area['city_pid']}")->find();
		$province=$cityModel->where("city_id={$city['city_pid']}")->find();
		$data['area']=$area['city_name'];
		$data['city']=$city['city_name'];
		$data['province']=$province['city_name'];
		//分配公司id
		View::with('gsid',$this->gsid);
		View::with('data',$data);
		View::with('cateData',$cateData);
		View::make();
	}

	//简历不合适操作
	public function pass(){
		//获得应聘者的uid
		$resumeUid=Q('get.uid',0,'intval');
		$statusModel=new \Common\Model\Status;
		//判断全部不合适操作
		if ($resumeUid) {
			$jid=Q('get.jid',0,'intval');
			$row=$statusModel->where("lg_user_uid=$resumeUid AND lg_job_jid=$jid")->update(['status'=>3,'endtime'=>time()]);
			if (!$row) View::error('没找到您要操作的简历');
			View::success('操作成功');
		}else{
			$uids=Q('get.uids');
			$jobModel=new \Common\Model\Job;
			$jids=$jobModel->where("lg_company_gsid={$this->gsid}")->lists('jid');
			$jids=implode(',',$jids);
			$row=$statusModel->where("lg_user_uid IN ({$uids}) AND lg_job_jid IN ({$jids})")->update(['status'=>3,'endtime'=>time()]);
			if (!$row) View::error('没找到您要操作的简历');
			View::success('操作成功');
		}
	}

	//将简历加入待定列表
	public function getReply(){
		//获得应聘者的uid
		$resumeUid=Q('get.uid',0,'intval');
		$statusModel=new \Common\Model\Status;
		if ($resumeUid) {
			$jid=Q('get.jid',0,'intval');
			$row=$statusModel->where("lg_user_uid=$resumeUid AND lg_job_jid=$jid")->update(['status'=>1,'dealtime'=>time()]);
			if (!$row) View::error('没找到您要操作的简历');
			View::success('操作成功');
		}else{
			View::error('没找到您要操作的简历');
		}
	}

	//通知面试
	public function getFacetime(){
		//获得应聘者的uid
		$resumeUid=Q('get.uid',0,'intval');
		$statusModel=new \Common\Model\Status;
		if ($resumeUid) {
			$jid=Q('get.jid',0,'intval');
			$row=$statusModel->where("lg_user_uid=$resumeUid AND lg_job_jid=$jid")->update(['status'=>2,'endtime'=>time()]);
			if (!$row) View::error('没找到您要操作的简历');
			View::success('操作成功');
		}else{
			View::error('没找到您要操作的简历');
		}
	}

	//删除不合适、已面试简历
	public function delResume(){
		//获得应聘者的uid
		$resumeUid=Q('get.uid',0,'intval');
		$statusModel=new \Common\Model\Status;
		//判断全部不合适操作
		if ($resumeUid) {
			$jid=Q('get.jid',0,'intval');
			$row=$statusModel->where("lg_user_uid=$resumeUid AND lg_job_jid=$jid")->update(['recycle'=>1]);
			if (!$row) View::error('没找到您要操作的简历');
			View::success('删除成功');
		}else{
			$uids=Q('get.uids');
			$jobModel=new \Common\Model\Job;
			$jids=$jobModel->where("lg_company_gsid={$this->gsid}")->lists('jid');
			$jids=implode(',',$jids);
			$row=$statusModel->where("lg_user_uid IN ({$uids}) AND lg_job_jid IN ({$jids})")->update(['recycle'=>1]);
			if (!$row) View::error('没找到您要操作的简历');
			View::success('删除成功');
		}
	}


}